<template>
    <div>
       <h3>门店信息录入</h3>
       <table class="table">
              <thead>   
                <tr>
                    <th>门店名称</th>
                    <th><input type="text" v-model="obj.storeName"></th>
                </tr>
                <tr>
                    <th>门店图片</th>
                    <th>
                        <img :src="axios.defaults.baseURL+obj.storeImg" style="width: 60px;height: 60px;">
                        <input type="file" @change="upload">
                    </th>
                </tr>
                <tr>
                    <th>门店状态</th>
                    <th>
                        <input type="radio" v-model="obj.storeStart" name="start" value="1">开店
                        <input type="radio" v-model="obj.storeStart" name="start" value="2">闭店
                    </th>
                </tr>
                <tr>
                    <th colspan="2">
                        <input type="button" value="录入" class="btn btn-info" @click="AddStore">
                    </th>
                </tr>
              </thead>
       </table>
       <a href="/showall">返回首页</a>
    </div>
</template>

<script setup lang="ts">
import axios from 'axios';
import { ref } from 'vue';
import { useRouter } from 'vue-router';
const router=useRouter()

//上传
const upload=(e:any)=>{
      let f=e.target.files[0]
      let fs=new FormData()
      fs.append('file',f)
      if(f.size>(2*1024*1024))
      {
        alert('图片大小不能超过2MB!')
        return;
      }
      if(f.type!="image/jpeg"&&f.type!="image/png"&&f.type!="image/webp")
      {
         alert('图片格式不正确!')
        return;
      }
      axios.post('/api/UpLoad/UpLoad',fs).then(res=>{
        obj.value.storeImg=res.data
      })
}

//响应录入数据
const obj=ref({
  "storeName": "",
  "storeImg": "",
  "storeStart": 1,
  storeTime:new Date
})
//录入
const AddStore=()=>{
    if(obj.value.storeName=="")
{
    alert('名称不能为空!')
        return;
}
if(obj.value.storeImg=="")
{
    alert('图片不能为空!')
        return;
}

    axios.post('/api/Store/AddStore',obj.value).then(res=>{
     if(res.data==-1)
     {
        alert('相同门店信息不能出现多次!!')
        return;
     }
     else if(res.data>0)
     {
        alert('录入成功!!')
        router.push('/showstore')
     }
    else
    {
        alert('录入失败!!')
        return;
    }
    })
}
</script>

<style scoped>

</style>